package cn.devices.controller;


import cn.devices.TbDmDto;
import cn.devices.dto.DmUserDto;
import cn.devices.entity.TbDm;
import cn.devices.entity.TbUser;
import cn.devices.model.JsonMessage;
import cn.devices.service.TbDmService;
import cn.devices.service.TbUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>a
 * <p>
 * 1.添加部门：tbDmAddDm
 * 2.删除部门：假删除 tbDmDeleteFalse,真删除 tbDmDeleteTrue
 * 3.修改部门信息：tbDmUpdate  添加字段 depCodeOriginal 原部门编号
 * 4.所有部门：tbDmList
 * 5.单个部门:tbDmSelect
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/tb-dm")
public class TbDmController {

    @Autowired
    private TbDmService tbDmService;

    @Autowired
    private TbUserService tbUserService;



    /**
     * 按部门编号查询
     *
     * @param pageIndex
     * @param pageSize
     * @param depCode
     * @return
     */
    @GetMapping("depUserList")
    public JsonMessage depUserList(@RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "10") int pageSize,
                                   String depCode) {
        JsonMessage message = new JsonMessage();
        PageInfo<TbUser> list = tbUserService.dpUserList(pageIndex, pageSize, depCode);
        message.setData(list);
        return message;
    }

    /**
     * 添加部门
     *
     * @param tbDm
     * @return
     */
    @PostMapping("tbDmAddDm")
    public JsonMessage tbDmAddDm(@RequestBody TbDm tbDm) {
        JsonMessage message = new JsonMessage();
        //判断是否已存在该部门
        if (tbDmService.count(tbDm.getDepCode(), tbDm.getDepName()) == 0) {
            tbDmService.tbDmAdd(tbDm);
            message.setData("添加成功");
        } else {
            //已存在
            message.setData("添加失败，部门编号或名称已存在");
        }
        return message;
    }

    /**
     * 假删除
     *
     * @param depCode 部门编号
     * @return
     */
    @GetMapping("tbDmDeleteFalse")
    public JsonMessage tbDmDeleteFalse(String depCode) {
        JsonMessage message = new JsonMessage();
        tbDmService.tbDmDeleteFalse(depCode);
        message.setData("删除成功");
        return message;
    }

    /**
     * 真删除
     *
     * @param depCode 部门编号
     * @return
     */
    @GetMapping("tbDmDeleteTrue")
    public JsonMessage tbDmDeleteTrue(String depCode) {
        JsonMessage message = new JsonMessage();
        tbDmService.tbDmDeleteTrue(depCode);
        message.setData("删除成功");
        return message;
    }

    /**
     * 更新
     *
     * @param tbDmDto
     * @return
     */
    @PostMapping("tbDmUpdate")
    public JsonMessage tbDmUpdate(@RequestBody TbDmDto tbDmDto) {
        JsonMessage message = new JsonMessage();
        try {
            if (tbDmService.countDmName(tbDmDto.getDepCodeOriginal(), tbDmDto.getDepName()) == 0) {
                tbDmService.tbDmUpdate(tbDmDto);
                message.setData("更新成功");
            } else {
                message.setData("该部门名称已存在");
            }

        } catch (DuplicateKeyException e) {
            message.setData("更新失败,该部门编号已存在");
        }


        return message;
    }

    /**
     * 所有部门
     *
     * @return
     */

    @GetMapping("tbDmList")
    public JsonMessage tbDmList(@RequestParam(defaultValue = "1") int pageIndex,
                                @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        PageInfo<TbDm> list = tbDmService.tbDmList(pageIndex, pageSize);
        message.setData(list);
        return message;
    }

    @GetMapping("tbDmReturnList")
    public JsonMessage tbDmReturnList() {
        JsonMessage message = new JsonMessage();
        message.setData(tbDmService.list());
        return message;
    }

    /**
     * @param depCode
     * @return
     */
    @GetMapping("tbDmSelect")
    public JsonMessage tbDmSelect(String depCode) {
        JsonMessage message = new JsonMessage();
        TbDm tbDm = tbDmService.tbDmSelect(depCode);
        message.setData(tbDm);
        return message;
    }

}
